package com.heima.behavior.Service.Impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.article.IFollowClient;
import com.heima.behavior.Service.LikesService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.Dtos.AllDto;
import com.heima.model.wemedia.utils.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
@Transactional
public class LikesServiceImpl implements LikesService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private IFollowClient followClient;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    /**
     * 点赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likes(AllDto dto) {

        if (dto.getArticleId() == null || dto.getEquipmentId() == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = followClient.selectOne(dto);

        Integer userId = AppThreadLocalUtils.getUser().getId();

        Map<String,Object> map = new HashMap<>();
        map.put("id",dto.getArticleId());

        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        mess.setArticleId(dto.getArticleId());

        if (dto.getOperation() == 0){

            //点赞
            cacheService.zAdd(BehaviorConstants.APUSER_LIKES_RELATION+userId+apArticle.getAuthorId(),"Likes",System.currentTimeMillis());
            map.put("type",true);
            mess.setAdd(1);

        } else {
            //取消点赞
            cacheService.zRemove(BehaviorConstants.APUSER_LIKES_RELATION+userId+apArticle.getAuthorId(),"Likes");
            map.put("type",false);
            mess.setAdd(-1);
        }

        //kafka数据口同步数据
//        kafkaTemplate.send(WmNewsMessageConstants.LIKES_BEHAVIOR,JSON.toJSONString(map));

        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));

        return ResponseResult.okResult("ok");
    }

    /**
     * 不喜欢
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult UnLikes(AllDto dto) {

        if (dto.getArticleId() == null || dto.getEquipmentId() == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = followClient.selectOne(dto);

        Integer userId = AppThreadLocalUtils.getUser().getId();
        if (dto.getType() == 0){

            //不喜欢
            cacheService.zAdd(BehaviorConstants.APUSER_UNLIKES_RELATION+userId+apArticle.getAuthorId(),"UNLikes",System.currentTimeMillis());
        }
        else {
            //取消不喜欢
            cacheService.zRemove(BehaviorConstants.APUSER_UNLIKES_RELATION+userId+apArticle.getAuthorId(),"UNLikes");
        }

        return ResponseResult.okResult("ok");
    }

    /**
     * 阅读
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(AllDto dto) {

        if (dto.getArticleId() == null || dto.getEquipmentId() == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = followClient.selectOne(dto);

        cacheService.hIncrBy(BehaviorConstants.APUSER_READBEHAVIOR_RELATION,apArticle.getId().toString(),1L);

        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(1);



        //kafka数据口同步数据
//        kafkaTemplate.send(WmNewsMessageConstants.READ_BEHAVIOR, dto.getArticleId().toString());
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));


        return ResponseResult.okResult("ok");
    }


}
